/**
 * Created by chenweicheng on 19/6/18.
 */
/**
 * Created by chenweicheng on 15/7/17.
 */
import React, {PureComponent} from 'react'
import {View, Text, StyleSheet, Image, TouchableOpacity, NativeAppEventEmitter, Platform, Alert} from 'react-native'

import {NavigationActions} from 'react-navigation'
import {px2dpH, px2dpW} from '../config/px2dp'
import Camera from 'react-native-camera';
import theme from '../config/theme'

export  default class ScanCodeScene extends PureComponent {
    static navigationOptions = ({navigation}) => ({


        headerTitle: '扫描设备',


        headerRight: <View/>,
        headerStyle: theme.headerStyle,


    });


    constructor() {
        super();


        this.getCode = false;

    }


    goNext(data) {


        if (!this.getCode) {
            this.getCode = true;
            console.log('true', this.props.navigation.state.params.type)
            this.props.navigation.navigate("ScanCodeResultScene", {
                data: data.data,
                type: this.props.navigation.state.params.type
            })

        } else {
            console.log('false');
            console.log(data)
        }
    }

    requestTypeList() {


    }

    componentDidMount() {

    }


    componentWillMount() {
//todo NativeAppEventEmitter.emit('aaa', 'aaa') 下个返回的页面需调这个
        NativeAppEventEmitter.addListener('aaa', (e) => {
            console.log('ScanCodeScene--------------DeviceEventEmitter')
            this.getCode = false;
        })
    }

    componentWillUnmount() {
        this.camera = null;
        NativeAppEventEmitter.removeListener('aaa')
        NativeAppEventEmitter.emit('gobacktohome', 'gobacktohome')
    }

    render() {
        console.log('ScanCodeScene--------------render')

        return (
            <View style={styles.container}>
                <View style={{height:px2dpH(236),width:px2dpH(236),marginTop:px2dpH(105)}}>

                    <Camera
                        ref={(cam) => {this.camera = cam;}}
                        style={[styles.preview, ]}
                        aspect={Camera.constants.Aspect.fill}
                        onBarCodeRead={(data) => {this.goNext(data)}}/>
                </View>
                <Image
                    style={{top:px2dpH(105),width:px2dpH(236),height:px2dpH(236),position:'absolute'}}
                    source={require('../img/scan.png')}/>

            </View>)
    }
}


const
    styles = StyleSheet.create({
        container: {
            flex: 1,
            backgroundColor: 'rgba(25, 25, 25, 0.71)',
            alignItems: 'center'

        }, fontCommon: {
            fontFamily: 'PingFang SC',
            fontWeight: 'normal',
            fontStyle: 'normal',
        }, preview: {
            flex: 1,
            justifyContent: 'flex-end',
            alignItems: 'center'
        },
        capture: {
            flex: 0,
            backgroundColor: '#fff',
            borderRadius: 5,
            color: '#000',
            padding: 10,
            margin: 40
        }


    });
